/* eslint-disable camelcase */
import { defineConfig, loadEnv } from 'vite'
import createVitePlugins from './vite/plugins'
import * as path from 'path'
const resolve = (p: string) => {
  return path.resolve(__dirname, p)
}

export default ({ mode }) => {
  const envConfig = loadEnv(mode, process.cwd())
  return defineConfig({
    plugins: createVitePlugins(),
    base: './',
    resolve: {
      alias: {
        '@': resolve('./src'),
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    },
    server: {
      host: envConfig.VITE_host,
      port: Number(envConfig.VITE_port) || 3000,
      proxy: {
        '/api': {
          target: 'http://127.0.0.1:8888',
          ws: true,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, ''),
        },
      },
    },
    build: {
      outDir: envConfig.VITE_fileName || 'dist',
      sourcemap: false,
      minify: 'terser',
      chunkSizeWarningLimit: 2000,
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      //   rollupOptions: {
      //     output: {
      //       manualChunks(id) {
      //         if (id.includes('node_modules')) {
      //           return id.toString().split('node_modules/')[1].split('/')[0].toString()
      //         }
      //       },
      //       chunkFileNames: chunkInfo => {
      //         const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/') : []
      //         const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]'
      //         return `js/${fileName}/[name].[hash].js`
      //       },
      //     },
      //   },
    },
  })
}
